<?php

namespace api\modules\v1\controllers\clinic;

use Yii;
use api\controllers\OnAuthController;
use common\helpers\CommonHelper;
use common\models\clinic\Project;
use api\modules\v1\forms\clinic\ProjectForm;

/**
 * Class ProjectController
 * @package api\modules\v1\controllers\clinic
 */
class ProjectController extends OnAuthController
{
    /**
     * @var Project
     */
    public $modelClass = Project::class;

    /**
     * @api {get} /v1/clinic/project/list 治疗项目列表
     * @apiName list
     * @apiGroup Clinic
     *
     * @apiParam {Number} page                           页码
     * @apiParam {Number} pageSize                       每页显示数量
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.page                   当前页码
     * @apiSuccess {Number}  data.pageSize               每页显示数
     * @apiSuccess {Number}  data.total                  总数
     * @apiSuccess {Array}   data.list                   治疗项目列表
     * @apiSuccess {Number}  data.list.id                治疗项目ID
     * @apiSuccess {Number}  data.list.project_id        项目ID
     * @apiSuccess {String}  data.list.name              治疗项目名称
     * @apiSuccess {String}  data.list.bind_yiyuan_name  奕远系统的治疗项目名称
     * @apiSuccess {String}  data.list.room_names        所属治疗室
     * @apiSuccess {Number}  data.list.status            状态
     * @apiSuccess {String}  data.list.status_str        状态值
     * @apiSuccess {String}  data.list.template          模板类型【1：308、2：311、3通用】
     * @apiSuccess {String}  data.list.template_str      模板类型值
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = $this->projectId;
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        // 查询结果
        $data = Yii::$app->services->clinicProject->getListPage($params, (int)$page, (int)$pageSize);

        // 格式化列表数据
        $data['list'] = Yii::$app->services->clinicProject->getListPageFormat($data['list']);

        return $data;
    }

    /**
     * @api {get} /v1/clinic/project/info 治疗项目详情
     * @apiName info
     * @apiGroup Clinic
     *
     * @apiParam {Number} id                                            治疗项目ID
     *
     * @apiSuccess {Object}     data                                    返回数据
     * @apiSuccess {Number}     data.id                                 治疗项目ID
     * @apiSuccess {String}     data.name                               治疗项目名称
     * @apiSuccess {String}     data.bind_yiyuan_name                   奕远系统的治疗项目名称
     * @apiSuccess {Number}     data.status                             状态
     * @apiSuccess {Number}     data.template                           模板类型【1：308、2：311、3通用】
     */
    public function actionInfo()
    {
        $id = (int)Yii::$app->request->get('id', 0);

        // 诊室信息
        $clinicProject = Yii::$app->services->clinicProject->findById($id);

        return [
            'id' => $clinicProject->id ?? 0,
            'name' => $clinicProject->name ?? '',
            'bind_yiyuan_name' => $clinicProject->bind_yiyuan_name ?? '',
            'status' => $clinicProject->status ?? Project::STATUS_ENABLE,
            'template' => $clinicProject->template ?? Project::TEMPLATE_GENERAL,
        ];
    }

    /**
     * @api {post} /v1/clinic/project/create 新增治疗项目
     * @apiName create
     * @apiGroup Clinic
     *
     * @apiParam {String} name                           治疗项目名称
     * @apiParam {Number} status                         状态【1启用、0停用】
     * @apiParam {String} bind_yiyuan_name               奕远系统的治疗项目名称
     * @apiParam {Number} template                       模板类型【1：308、2：311、3通用】
     * @apiParam {Number} sort                           排序
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.id                     治疗项目ID
     */
    public function actionCreateProject()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        if (!isset($params['sort']) || !$params['sort']) {
            $params['sort'] = Yii::$app->services->clinicProject->getMaxSort($this->projectId);
        }

        $model = new ProjectForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->clinicProject->create($model);
    }

    /**
     * @api {post} /v1/clinic/project/update 编辑治疗项目
     * @apiName update
     * @apiGroup Clinic
     *
     * @apiParam {Number} id                             治疗项目ID
     * @apiParam {String} name                           治疗项目名称
     * @apiParam {Number} status                         状态【1启用、0停用】
     * @apiParam {String} bind_yiyuan_name               奕远系统的治疗项目名称
     * @apiParam {Number} template                       模板类型【1：308、2：311、3通用】
     * @apiParam {Number} sort                           排序
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.id                     治疗项目ID
     */
    public function actionUpdateProject()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        if (!isset($params['sort']) || !$params['sort']) {
            $params['sort'] = Yii::$app->services->clinicProject->getMaxSort($this->projectId);
        }

        $model = new ProjectForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->clinicProject->edit($model);
    }

    /**
     * @api {post} /v1/clinic/project/delete 删除治疗项目
     * @apiName delete
     * @apiGroup Clinic
     *
     * @apiParam {Number} id                             治疗项目ID
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.id                     治疗项目ID
     */
    public function actionDeleteProject()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        $model = new ProjectForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->clinicProject->delete($model);
    }

    /**
     * @api {post} /v1/clinic/project/update-status 编辑治疗项目状态
     * @apiName update-status
     * @apiGroup Clinic
     *
     * @apiParam {Number} id                             治疗项目ID
     * @apiParam {Number} status                         状态【1启用、0停用】
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.id                     治疗项目ID
     */
    public function actionUpdateProjectStatus()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        $model = new ProjectForm();
        $model->setScenario('edit_status');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->clinicProject->editStatus($model);
    }

    public function actionClinicTemplate()
    {
        $templates = Project::$templates;
        $list = [];
        foreach ($templates as $k => $t) {
            $list[] = [
                'title' => $t,
                'value' => $k
            ];
        }
        return $list;
    }
}